﻿using DX_Recognize.Disposition;
using DX_Recognize.Utils;
using System;
using ZwSoft.ZwCAD.Runtime;
using System.Windows.Forms;
using ZWCAD;
using DX_Recognize.AuxiliaryTool;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using ZwSoft.ZwCAD.Geometry;
using DX_Recognize.Entitys;
using System.Collections.Generic;
using Application = ZwSoft.ZwCAD.ApplicationServices.Application;
using System.Xml.Linq;
using ZwSoft.ZwCAD.Colors;
using ZwSoft.ZwCAD.GraphicsInterface;
using ZwSoft.ZwCAD.GraphicsSystem;

namespace DX_Recognize
{
    public class Commad : IExtensionApplication
    {
        public Boolean DispositionFlag = false;
        RecognizeUtils Recognize = new RecognizeUtils();
        Mould mould = new Mould();

        public void Initialize()
        {
            ZcadMenuGroups MenuGroups = (ZWCAD.ZcadMenuGroups)ZwSoft.ZwCAD.ApplicationServices.Application.MenuGroups;
            ZcadMenuBar MenuBar = (ZWCAD.ZcadMenuBar)ZwSoft.ZwCAD.ApplicationServices.Application.MenuBar;
            ZcadPopupMenu popupMenu = MenuGroups.Item(0).Menus.Add("模架识别菜单");//顶部菜单项
            popupMenu.AddMenuItem(popupMenu.Count, "GUI界面", "GETMJDATA ");
            popupMenu.InsertInMenuBar(MenuBar.Count);
        }

        //加载界面
        [CommandMethod("GETMJDATA")]
        public void CADWin()
        {
            if (!DispositionFlag)
            {
                mould.SetDisposition();
                DispositionFlag = true;
            }
            RecognizeUtils.dataForm.WindowState = FormWindowState.Normal;
            Recognize.showForm();
        }

        //绘制图形中心线
        [CommandMethod("ACL")]
        public static void DrawCenterLine()
        {
            RecognizeUtils.DrawCenterLine();
        }

        //抓取客图模号
        public static void GetCADModeNum()
        {
            RecognizeUtils.GrabModeNumber();
        }

        //绘制图形中心线
        [CommandMethod("WCArc")]
        public static void DrawArc()
        {
            DWGDrawFrameArc(new CAD_Point(0, 0, 0), 45, 78, 60);
        }

        static double DegreesToRadians(double degrees)
        {
            return degrees * (Math.PI / 180);
        }

        public static void DWGDrawFrameArc(CAD_Point arcCPoint, double startAngle, double endAngle, double Radius)
        {
            Document doc = ZwSoft.ZwCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            try
            {
                using (DocumentLock DocLock = doc.LockDocument())
                {
                    Point3d center = new Point3d(arcCPoint.X, arcCPoint.Y, 0);
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        BlockTableRecord modelSpace = tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                        double startRad = DegreesToRadians(startAngle);
                        double endRad = DegreesToRadians(endAngle);
                        Arc arc = new Arc(center, Radius, startRad, endRad);
                        modelSpace.AppendEntity(arc);
                        tr.AddNewlyCreatedDBObject(arc, true);
                        tr.Commit();
                    }
                }
            }
            catch (ZwSoft.ZwCAD.Runtime.Exception ex)
            {
                throw;
            }
        }
        public void Terminate()
        {
            throw new NotImplementedException();
        }
       
    }
}
